package com.zbf.service;

import com.zbf.dao.MenuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/2  11:29
 * 描述:
 */
@Component
public class MenuService {

    @Autowired
    private MenuDao menuDao;


    public List<Map<String,Object>> getUserMenus(String userId){

        Map<String,Object> map=new HashMap<>();
        map.put("parentId",0);
        map.put("leval",1);
        map.put("userId",userId);
        //查询第一级别
        List<Map<String, Object>> menu1 = menuDao.getMenuList(map);

        getNextMenu(menu1,userId);

        return menu1;
    }


    public void getNextMenu( List<Map<String, Object>> menu,String userId){

        for (Map<String, Object> map : menu) {
            //下级查询条件
            Map<String,Object> mapnext=new HashMap<>();
            mapnext.put("parentId",map.get("code"));
            mapnext.put("leval",Integer.valueOf(map.get("leval").toString())+1);
            mapnext.put("userId",userId);
            List<Map<String, Object>> menu2 = menuDao.getMenuList(mapnext);
            if(menu2.size()>0){
                //递归查询下一级
                map.put("menuList",menu2);
                this.getNextMenu(menu2,userId);
            }else{
                continue;
            }

        }

    }


    /**
     * 作者: LCG
     * 日期: 2021/6/4  15:51
     * 描述: 获取所有的菜单
     * @Param [userId]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    public List<Map<String,Object>> getAllUserMenus(){

        Map<String,Object> map=new HashMap<>();
        map.put("parentId",0);
        map.put("leval",1);
        //查询第一级别
        List<Map<String, Object>> menu1 = menuDao.getAllMenuList(map);

        getAllNextMenu(menu1);

        return menu1;
    }


    public void getAllNextMenu( List<Map<String, Object>> menu){

        for (Map<String, Object> map : menu) {
            //下级查询条件
            Map<String,Object> mapnext=new HashMap<>();
            mapnext.put("parentId",map.get("code"));
            mapnext.put("leval",Integer.valueOf(map.get("leval").toString())+1);
            List<Map<String, Object>> menu2 = menuDao.getAllMenuList(mapnext);
            if(menu2.size()>0){
                //递归查询下一级
                map.put("menuList",menu2);
                this.getAllNextMenu(menu2);
            }else{
                continue;
            }

        }

    }



    @Transactional(rollbackFor = Exception.class)
    public int addMenu(Map<String,String> map){

        return menuDao.addMenu(map);
    }


}
